package com.zhihuishu;

import java.io.*;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

/**
 * @Author: wangjinliang
 * @Date: 2019/5/29 14:53
 */
public class AliyunLogDemo {
    public static void main(String[] args) throws Exception {
        List<String> logList = new ArrayList<>(1000000);

        String filepath = "E:\\workplace\\aliyunlog\\log";
        File file = new File(filepath);
        if (file.isDirectory()) {
            String[] fileList = file.list();
            for (int i = 0; i < fileList.length; i++) {
                File realFile = new File(filepath + "\\" + fileList[i]);
                BufferedReader br = new BufferedReader(new FileReader(realFile));

                String result;
                boolean first = true;
                while ((result = br.readLine()) != null) {
                    if (first) {
                        first = false;
                        continue;
                    }
                    logList.add(result);
                }
                br.close();
            }
        } else {
            System.out.println("不是文件夹");
        }
        ResourceBundle rb = ResourceBundle.getBundle("db");
        String driver = rb.getString("jdbc.driver");
        String url = rb.getString("jdbc.url");
        String username = rb.getString("jdbc.username");
        String password = rb.getString("jdbc.password");
        Class.forName(driver);
        Connection connect = DriverManager.getConnection(url, username, password);
        Statement statement = connect.createStatement();

        logList.forEach(line -> {
            int userIdStartIndex = line.indexOf("见面课签到,学生[");
            int userIdEndIndex = line.indexOf("],直播课[");
            String userId = line.substring(userIdStartIndex + "见面课签到,学生[".length(), userIdEndIndex);
            int liveCourseIdStartIndex = line.indexOf("],直播课[");
            int liveCourseIdEndIndex = line.indexOf("],签到耗时日志记录,签到限制条件判断,结束,耗时");
            String liveCourse = line.substring(liveCourseIdStartIndex + "],直播课[".length(), liveCourseIdEndIndex);
            String sql = "INSERT INTO log(user_id, live_course_id) values (" + userId + ", " + liveCourse +")";
            try {
                statement.execute(sql);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        });
        connect.close();
    }
}
